<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-page-header content="修改商家" style="padding-bottom: 50px" @back="goBack" />
      <el-form ref="form" :model="form" label-width="120px" label-position="right" label-suffix="：" class="form_style" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商家名称" prop="title">
              <el-input v-model="form.title" style="width: 400px" placeholder="商家名称" clearable />
            </el-form-item>
            <el-form-item label="商家详细地址" prop="store_address">
              <el-input v-model="form.store_address" style="width: 400px" placeholder="商家地址" clearable />
            </el-form-item>
            <el-form-item label="经度" prop="longitude">
              <el-input
                v-model="form.longitude"
                clearable
                style="width: 180px; margin-right: 30px"
                placeholder="经度"
                onkeyup="value=value.replace(/[^1-9]{0,1}(\d*(?:\.\d{0,5})?).*$/g, '$1')"
              />
            </el-form-item>
            <el-form-item label="纬度" prop="latitude">
              <el-input
                v-model="form.latitude"
                style="width: 180px"
                placeholder="纬度"
                clearable
                onkeyup="value=value.replace(/[^1-9]{0,1}(\d*(?:\.\d{0,5})?).*$/g, '$1')"
              />
            </el-form-item>
            <div class="tips">（请输入准确的经纬度信息，用于微信小程序地图定位）</div>
            <el-form-item label="营业时间" prop="business_hours">
              <el-time-picker
                v-model="form.business_hours"
                style="width: 400px"
                is-range
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                placeholder="选择时间范围"
                value-format="HH:mm"
              />
            </el-form-item>
            <el-form-item label="联系电话" prop="store_phone">
              <el-input
                v-model="form.store_phone"
                style="width: 400px"
                placeholder="联系电话"
                clearable
                onkeyup="value=value.replace(/[^\d]/g,'').replace(/^0{1,}/g,'')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商家公告" prop="announcement">
              <el-input v-model="form.announcement" type="textarea" :rows="3" style="width: 400px" placeholder="商家公告" />
            </el-form-item>
            <el-form-item label="配送信息" prop="delivery_info">
              <el-input v-model="form.delivery_info" type="textarea" style="width: 400px" :rows="3" placeholder="配送信息" />
            </el-form-item>
            <el-form-item label="商家图片" prop="store_imgs">
              <new-upload
                :file_source="'store_imgs'"
                :upload-type="3"
                :format="'.jpg,.jpeg,.png,.JPG,.JPEG,.PNG'"
                :format-txt="'最多上传3张,支持格式：.jpg,.jpeg,.png,.JPG,.JPEG,.PNG'"
                :limit-num="3"
                @reUpload="getUpladImg"
                :fileArr="form.store_imgs"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="footer-btn">
        <el-button type="primary" style="margin-right: 10px" @click="edit" size="medium">修改</el-button>
        <el-button @click="goBack" size="medium">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
  import NewUpload from '@/components/NewUpload'
  import { store_detail, edit_store } from '@/api/store'
  export default {
    components: { NewUpload },
    data() {
      return {
        form: {
          store_imgs: [],
        },
        rules: {
          title: [{ required: true, message: '请输入商家名称', trigger: 'blur' }],
          store_address: [{ required: true, message: '请输入商家详细地址', trigger: 'blur' }],
          longitude: [{ required: true, message: '请输入经度', trigger: 'blur' }],
          latitude: [{ required: true, message: '请输入纬度', trigger: 'blur' }],
          business_hours: [{ required: true, message: '请输入营业时间', trigger: 'change' }],
          store_phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
          store_imgs: [{ required: true, message: '请上传商家图片', trigger: 'change' }],
        },
      }
    },
    mounted() {
      this.get_info()
    },
    methods: {
      get_info() {
        store_detail({ id: this.$route.query.id })
          .then()
          .then(({ data }) => {
            console.log('data: ', data)
            // 整理时间格式
            data.business_hours = data.business_hours.split('-')
            data.store_imgs = data.store_imgs.split(',')
            this.form = data
          })
      },
      edit() {
        console.log(this.form)
        this.$refs.form.validate((valid) => {
          if (valid) {
            edit_store(this.form).then((data) => {
              console.log('data: ', data)
              if (data.code == 200) {
                this.$message.success('修改成功')
                this.goBack()
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            return false
          }
        })
      },
      goBack() {
        this.$router.push('/business/list')
      },
      getUpladImg(file_list) {
        this.form.store_imgs = file_list
        console.log(' this.form.store_imgs : ', this.form.store_imgs)
      },
    },
  }
</script>

<style scoped>
  .tips {
    margin-left: 120px;
    margin-top: 0;
    margin-bottom: 20px;
    font-size: 14px;
    color: rgb(235, 44, 44);
  }
  .footer-btn {
    text-align: center;
    margin-top: 50px;
  }
</style>
